package br.com.massai.model.dao;

import java.util.List;

import javax.ejb.Stateless;

import br.com.massai.model.entity.Entry;
import br.com.massai.model.entity.EntryItem;
import br.com.massai.model.exceptions.SystemException;

@Stateless
public class EntryItemDAO extends GenericDAO<EntryItem> implements EntryItemRepository {

	private static final long serialVersionUID = 4383291197688461892L;

	public EntryItemDAO(){
		super(EntryItem.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EntryItem> getEntryItemsByEntry(Entry entry) throws SystemException {
		return em.createQuery("SELECT e FROM EntryItem e WHERE e.entry =:entry").setParameter("entry", entry).getResultList();
	}

	@Override
	public void deleteNative(EntryItem entryItem) throws SystemException {
		em.createNativeQuery("DELETE FROM entry_item WHERE id =:id").setParameter("id", entryItem.getId()).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EntryItem> getEntryItemsForFile(String empCode, String epgCode, String accrualBasis) throws SystemException {
		return em.createQuery("SELECT e FROM EntryItem e WHERE e.entry.employee.empCodigo =:empCode AND e.entry.employee.epgCodigo =:epgCode AND e.entry.spreadsheet.accrualBasis =:accrualBasis")
				.setParameter("empCode", empCode)
				.setParameter("epgCode", epgCode)
				.setParameter("accrualBasis", accrualBasis)
				.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<EntryItem> getEntryItemsForFile(String empCode, String codeDepartment, String epgCode, String accrualBasis) throws SystemException {
		return em.createQuery("SELECT e FROM EntryItem e WHERE e.entry.employee.empCodigo =:empCode AND e.entry.spreadsheet.department.codeDepartment LIKE :codeDepartment AND e.entry.employee.epgCodigo =:epgCode AND e.entry.spreadsheet.accrualBasis =:accrualBasis")
				.setParameter("empCode", empCode)
				.setParameter("codeDepartment", codeDepartment + "%")
				.setParameter("epgCode", epgCode)
				.setParameter("accrualBasis", accrualBasis)
				.getResultList();
	}
}